#!/bin/bash

# Slackware build script for jfbview

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# This doesn't depend on mupdf or mupdf-legacy because it includes its
# own bundled mupdf source.

# 20230104 bkw: BUILD=2
# - fix imlib2 builds (broke with recent imlib2 update to 1.10.0).
# - fix PKGTYPE mess.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=jfbview
VERSION=${VERSION:-0.6.0}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION-full-source
unzip $CWD/$PRGNAM-$VERSION-full-source.zip
cd $PRGNAM-$VERSION-full-source
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# fix a typo.
sed -i 's,Plase,Please,' src/main.cpp

# hardcoded man and doc paths.
sed -i -e 's,share/man,man,g' \
       -e "s,share/doc/$PRGNAM,doc/$PRGNAM-$VERSION,g" \
       doc/CMakeLists.txt

# no autodetect for imlib2, help it out some.
# 20230104 bkw: imlib2 no longer has /usr/bin/imlib2-config, which
# the FindImlib2.cmake in jfbview's source wants. Work around it by
# manually setting the cmake vars it would automatically set.
if [ "${IMLIB:-yes}" = "yes" ] && pkg-config --exists imlib2; then
  IMLIB_OPT="-DENABLE_IMAGE_SUPPORT=ON -DIMLIB2_FOUND=ON -DIMLIB2_LIBRARIES=-lImlib2"
else
  IMLIB_OPT="-DENABLE_IMAGE_SUPPORT=OFF"
fi

mkdir -p build
cd build
  cmake \
    $IMLIB_OPT \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=Release ..
  make VERBOSE=1
  make install/strip DESTDIR=$PKG
cd ..

# when building without imlib2, we end up with two almost-identical copies
# of the binary, weighing in at 36MB apiece. use symlinks instead.
rm -f $PKG/usr/bin/jfbpdf $PKG/usr/man/man1/jfbpdf.1.gz
ln -s $PRGNAM $PKG/usr/bin/jfbpdf
ln -s $PRGNAM.1.gz $PKG/usr/man/man1/jfbpdf.1.gz

# upstream's got a nice man page for the main application, but nothing
# for jpdf(cat|grep), so I wrote these.
gzip -9c < $CWD/jpdfcat.1 > $PKG/usr/man/man1/jpdfcat.1.gz
gzip -9c < $CWD/jpdfgrep.1 > $PKG/usr/man/man1/jpdfgrep.1.gz

cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

WITHIM=WITHOUT
objdump -p $PKG/usr/bin/$PRGNAM | grep -q 'NEEDED.*libImlib2' && WITHIM=with

mkdir -p $PKG/install
sed "s,@WITHIM@,$WITHIM," $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
